package com.mdd.front.service.salesman.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mdd.common.constant.GlobalConstant;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.salesman.Salesman;
import com.mdd.common.entity.salesman.UserSalesman;
import com.mdd.common.mapper.salesman.UserSalesmanMapper;
import com.mdd.common.utils.TimeUtil;
import com.mdd.front.service.impl.UserServiceImpl;
import com.mdd.front.service.salesman.ISalesmanService;
import com.mdd.front.service.salesman.IUserSalesmanService;
import com.mdd.front.validate.PageParam;
import com.mdd.front.validate.salesman.UserSalesmanParam;
import com.mdd.front.vo.salesman.UserSalesmanDetailVo;
import com.mdd.front.vo.salesman.UserSalesmanListVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 用户_推销员关联实现类
 */
@Service
public class UserSalesmanServiceImpl extends ServiceImpl<UserSalesmanMapper, UserSalesman> implements IUserSalesmanService {

	@Resource
	private UserServiceImpl userService;

	@Resource
	private ISalesmanService salesmanService;

	/**
	 * 用户_推销员关联列表
	 *
	 * @param pageParam 分页参数
	 * @param params    搜索参数
	 * @return PageResult<UserSalesmanListVo>
	 * @author 蒋海省
	 */
	@Override
	public PageResult<UserSalesmanListVo> list(PageParam pageParam, Map<String, String> params) {
		Integer page = pageParam.getPageNo();
		Integer limit = pageParam.getPageSize();

		QueryWrapper<UserSalesman> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("is_delete", 0);
		queryWrapper.orderByDesc("id");

		this.baseMapper.setSearch(queryWrapper, params, new String[]{
				"=:userId@user_id:long",
				"=:salesmanId@salesman_id:long",
				"datetime:createTimeStart-createTimeEnd@create_time:str",
				"datetime:updateTimeStart-updateTimeEnd@update_time:str",
				"datetime:deleteTimeStart-deleteTimeEnd@delete_time:str",
				"=:isDelete@is_delete:long",
		});

		IPage<UserSalesman> iPage = this.baseMapper.selectPage(new Page<>(page, limit), queryWrapper);

		List<UserSalesmanListVo> list = new LinkedList<>();
		for (UserSalesman item : iPage.getRecords()) {
			UserSalesmanListVo vo = new UserSalesmanListVo();
			BeanUtils.copyProperties(item, vo);
			vo.setCreateTime(TimeUtil.timestampToDate(item.getCreateTime()));
			list.add(vo);
		}

		return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
	}

	/**
	 * 用户_推销员关联详情
	 *
	 * @param id 主键参数
	 * @return UserSalesman
	 * @author 蒋海省
	 */
	@Override
	public UserSalesmanDetailVo detail(Integer id) {
		UserSalesman model = this.baseMapper.selectOne(
				new QueryWrapper<UserSalesman>()
						.eq("id", id)
						.eq("is_delete", 0)
						.last("limit 1"));

		Assert.notNull(model, "数据不存在");

		UserSalesmanDetailVo vo = new UserSalesmanDetailVo();
		BeanUtils.copyProperties(model, vo);
		vo.setUpdateTime(TimeUtil.timestampToDate(model.getUpdateTime()));
		vo.setDeleteTime(TimeUtil.timestampToDate(model.getDeleteTime()));
		return vo;
	}

	/**
	 * 用户_推销员关联新增
	 *
	 * @param userSalesmanParam 参数
	 * @author 蒋海省
	 */
	@Override
	public void add(UserSalesmanParam userSalesmanParam) {
		UserSalesman model = new UserSalesman();
		model.setUserId(userSalesmanParam.getUserId());
		model.setSalesmanId(userSalesmanParam.getSalesmanId());
		model.setCreateTime(System.currentTimeMillis() / 1000);
		this.baseMapper.insert(model);
	}

	/**
	 * 用户_推销员关联编辑
	 *
	 * @param userSalesmanParam 参数
	 * @author 蒋海省
	 */
	@Override
	public void edit(UserSalesmanParam userSalesmanParam) {
		UserSalesman model = this.baseMapper.selectOne(
				new QueryWrapper<UserSalesman>()
						.eq("id", userSalesmanParam.getId())
						.eq("is_delete", 0)
						.last("limit 1"));

		Assert.notNull(model, "数据不存在!");

		model.setId(userSalesmanParam.getId());
		model.setUserId(userSalesmanParam.getUserId());
		model.setSalesmanId(userSalesmanParam.getSalesmanId());
		this.baseMapper.updateById(model);
	}

	/**
	 * 用户_推销员关联删除
	 *
	 * @param id 主键ID
	 * @author 蒋海省
	 */
	@Override
	public void del(Integer id) {
		UserSalesman model = this.baseMapper.selectOne(
				new QueryWrapper<UserSalesman>()
						.eq("id", id)
						.eq("is_delete", 0)
						.last("limit 1"));

		Assert.notNull(model, "数据不存在!");

		model.setIsDelete(1);
		model.setDeleteTime(System.currentTimeMillis() / 1000);
		this.baseMapper.updateById(model);
	}

	@Override
	public PageResult<UserSalesmanDetailVo> getUserDetail(Integer userId, PageParam pageParam) {
		Integer page = pageParam.getPageNo();
		Integer limit = pageParam.getPageSize();

		Salesman one = salesmanService.getOne(new LambdaQueryWrapper<Salesman>()
				.eq(Salesman::getUserId, userId)
				.eq(Salesman::getIsDelete, GlobalConstant.NOT_DELETE));
		Assert.notNull(one, "你不是推销员");
		LambdaQueryWrapper<UserSalesman> lambdaQueryWrapper = new LambdaQueryWrapper<UserSalesman>()
				.eq(UserSalesman::getSalesmanId, one.getId())
				.eq(UserSalesman::getIsDelete, GlobalConstant.NOT_DELETE);
		Page<UserSalesman> userSalesmanPage = this.baseMapper.selectPage(new Page<>(page, limit), lambdaQueryWrapper);
		ArrayList<UserSalesmanDetailVo> userSalesmanDetailVos = new ArrayList<>();
		userSalesmanPage.getRecords().forEach(userSalesman -> {
			Integer userId1 = userSalesman.getUserId();
			UserSalesmanDetailVo userSalesmanDetailVo = new UserSalesmanDetailVo();
			BeanUtils.copyProperties(userSalesman, userSalesmanDetailVo);
			userSalesmanDetailVo.setUserName(userService.getById(userId1).getNickname());
			userSalesmanDetailVo.setCreateTime(TimeUtil.timestampToDate(userSalesman.getCreateTime(), "yyyy-MM-dd"));
			userSalesmanDetailVos.add(userSalesmanDetailVo);
		});
		return PageResult.iPageHandle(userSalesmanPage.getTotal(), userSalesmanPage.getCurrent(), userSalesmanPage.getSize(), userSalesmanDetailVos);
	}
}
